package com.gomore.opple.data.remote.retrofit;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gomore.opple.BuildConfig;
import com.gomore.opple.data.remote.api.WxApi;
import com.gomore.opple.utils.RetrofitUtils;

import java.text.SimpleDateFormat;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;

/**
 * Created by asus on 2017/6/8.
 */
public class WxRetrofitManager {
    private static WxRetrofitManager wxRetrofitManager;
    private Retrofit mRetrofit;

    private WxRetrofitManager() {
        initRetrofit();
    }

    public static synchronized WxRetrofitManager getInstance() {
        if (wxRetrofitManager == null) {
            wxRetrofitManager = new WxRetrofitManager();
        }
        return wxRetrofitManager;
    }

    private void initRetrofit() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        mRetrofit = new Retrofit.Builder()
                .baseUrl(BuildConfig.WX_HOST)
                .addConverterFactory(retrofit2.converter.jackson.JacksonConverterFactory.create(objectMapper))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(RetrofitUtils.genericWxClient())
                .build();
    }

    /**
     * 创建接口服务
     *
     * @return arApi
     */
    public WxApi createService() {
        return mRetrofit.create(WxApi.class);
    }
}
